home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Java2D / ToolBar.java < prev   
Encoding:
Java Source  |  1998-12-01  |  8.0 KB  |  248 lines

  1. /*
  2.  * @(#)ToolBar.java    1.22  98/09/22
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import java.awt.*;
  17. import java.awt.print.PrinterJob;
  18. import javax.swing.*;
  19. import java.awt.event.*;
  20. import java.net.URL;
  21.  
  22.  
  23. /**
  24.  * ToolBar to control individual demo graphic attributes.  Also, control for
  25.  * start & stop on animated demos; control for cloning the demo; control for
  26.  * printing the demo.
  27.  */
  28. public class ToolBar extends JPanel implements ActionListener, Runnable {
  29.  
  30.     private static ImageIcon stopIcon = 
  31.             new ImageIcon(ToolBar.class.getResource("images/stop.gif"));
  32.     private static ImageIcon startIcon = 
  33.             new ImageIcon(ToolBar.class.getResource("images/start.gif"));
  34.     private static Font font = new Font("serif", Font.PLAIN, 10);
  35.  
  36.     private DemoSurface surface;
  37.     private JButton printB;
  38.     private JToolBar toolbar;
  39.     private Thread thread;
  40.  
  41.     public JComboBox imgTypeCombo;
  42.     public JButton renderB, aliasB;
  43.     public JButton textureB, compositeB;
  44.     public JButton startStopB;
  45.     public JButton cloneB;
  46.     public boolean issueRepaint = true;
  47.     public boolean verboseFlag;
  48.  
  49.  
  50.     public ToolBar(DemoSurface surface) {
  51.  
  52.         this.surface = surface;
  53.  
  54.         toolbar = new JToolBar();
  55.  
  56.         String s = surface.AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON 
  57.             ? "On" : "Off";
  58.         aliasB = addTool( "A", "Antialiasing " + s, this);
  59.  
  60.         s = surface.Rendering == RenderingHints.VALUE_RENDER_SPEED
  61.             ? "Speed" : "Quality";
  62.         renderB = addTool("R", "Rendering " + s, this);
  63.  
  64.         s = surface.texture != null ? "On" : "Off";
  65.         textureB = addTool("T", "Texture " + s, this);
  66.  
  67.         s = surface.composite != null ? "On" : "Off";
  68.         compositeB = addTool("C", "Composite " + s, this);
  69.  
  70.         printB = addTool("print.gif", "Print the Surface", this);
  71.  
  72.         if (surface instanceof AnimatingContext || surface.observerRunning) {
  73.             startStopB = addTool("start.gif", "Animation Running", this);
  74.         }
  75.  
  76.  
  77.         imgTypeCombo = new JComboBox();
  78.         imgTypeCombo.setFont(font);
  79.         imgTypeCombo.setBackground(Color.lightGray);
  80.         imgTypeCombo.addItem("Auto Screen");
  81.         imgTypeCombo.addItem("On Screen");
  82.         imgTypeCombo.addItem("Off Screen");
  83.         imgTypeCombo.addItem("INT_RGB");
  84.         imgTypeCombo.addItem("INT_ARGB");
  85.         imgTypeCombo.addItem("INT_ARGB_PRE");
  86.         imgTypeCombo.addItem("INT_BGR");
  87.         imgTypeCombo.addItem("3BYTE_BGR");
  88.         imgTypeCombo.addItem("4BYTE_ABGR");
  89.         imgTypeCombo.addItem("4BYTE_ABGR_PRE");
  90.         imgTypeCombo.addItem("USHORT_565_RGB");
  91.         imgTypeCombo.addItem("USHORT_555_RGB");
  92.         imgTypeCombo.addItem("BYTE_GRAY");
  93.         imgTypeCombo.addItem("USHORT_GRAY");
  94.         imgTypeCombo.addItem("BYTE_BINARY");
  95.         imgTypeCombo.setPreferredSize(new Dimension(100, 20));
  96.         imgTypeCombo.setSelectedIndex(surface.imageType);
  97.         imgTypeCombo.addActionListener(this);
  98.  
  99.         setLayout(new FlowLayout());
  100.         setBackground(Color.gray);
  101.         add(toolbar);
  102.         add(imgTypeCombo);
  103.     }
  104.  
  105.  
  106.     public JButton addTool(String str, 
  107.                            String toolTip,
  108.                            ActionListener al) {
  109.         JButton b = null;
  110.         if (str.indexOf(".") == -1) {
  111.             b = (JButton) toolbar.add(new JButton(str));
  112.             if (toolTip.equals("Rendering Quality") ||
  113.                     toolTip.equals("Antialiasing On") ||
  114.                         toolTip.equals("Texture On")  ||
  115.                             toolTip.equals("Composite On")) {
  116.                 b.setBackground(Color.green);
  117.                 b.setSelected(true);
  118.             } else {
  119.                 b.setBackground(Color.lightGray);
  120.                 b.setSelected(false);
  121.             }
  122.         } else {
  123.             URL url = ToolBar.class.getResource("images/" + str);
  124.             b = (JButton) toolbar.add(new JButton(new ImageIcon(url)));
  125.             b.setSelected(true);
  126.         }
  127.         b.setToolTipText(toolTip);
  128.         b.addActionListener(al);
  129.         return b;
  130.     }
  131.  
  132.  
  133.     public void actionPerformed(ActionEvent e) {
  134.         Object obj = e.getSource();
  135.         if (obj.equals(printB)) {
  136.             start();
  137.             return;
  138.         }
  139.         if (obj instanceof JButton) {
  140.             JButton b = (JButton) obj;
  141.             b.setSelected(!b.isSelected());
  142.             if (b.getIcon() == null) {
  143.                 b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  144.             }
  145.         }
  146.         if (obj.equals(startStopB)) {
  147.             if (startStopB.getToolTipText().equals("Animation Running")) {
  148.                 startStopB.setIcon(stopIcon);
  149.                 startStopB.setToolTipText("Animation Stopped");
  150.                 surface.observerRunning = false;
  151.                 surface.stop();
  152.             } else {
  153.                 startStopB.setIcon(startIcon);
  154.                 startStopB.setToolTipText("Animation Running");
  155.                 surface.observerRunning = true;
  156.                 surface.start();
  157.             }
  158.         } else if (obj.equals(aliasB)) {
  159.             if (aliasB.getToolTipText().equals("Antialiasing On")) {
  160.                 aliasB.setToolTipText("Antialiasing Off");
  161.             } else {
  162.                 aliasB.setToolTipText("Antialiasing On");
  163.             }
  164.             surface.setAntiAlias(aliasB.isSelected());
  165.         } else if (obj.equals(renderB)) {
  166.             if (renderB.getToolTipText().equals("Rendering Quality")) {
  167.                 renderB.setToolTipText("Rendering Speed");
  168.             } else {
  169.                 renderB.setToolTipText("Rendering Quality");
  170.             }
  171.             surface.setRendering(renderB.isSelected());
  172.         } else if (obj.equals(textureB)) {
  173.             Object texture = null;
  174.             if (textureB.getToolTipText().equals("Texture On")) {
  175.                 textureB.setToolTipText("Texture Off");
  176.                 surface.setTexture(null);
  177.                 surface.clearSurface = true;
  178.             } else {
  179.                 textureB.setToolTipText("Texture On");
  180.                 surface.setTexture(TextureChooser.texture);
  181.             }
  182.         } else if (obj.equals(compositeB)) {
  183.             if (compositeB.getToolTipText().equals("Composite On")) {
  184.                 compositeB.setToolTipText("Composite Off");
  185.             } else {
  186.                 compositeB.setToolTipText("Composite On");
  187.             }
  188.             surface.setComposite(compositeB.isSelected());
  189.         } else if (obj.equals(imgTypeCombo)) {
  190.             surface.setImageType(imgTypeCombo.getSelectedIndex());
  191.         }
  192.  
  193.         if (issueRepaint && surface.thread != null) {
  194.             if (surface.sleepAmount != 0) {
  195.                 surface.thread.interrupt();
  196.             }
  197.         } else if (issueRepaint) {
  198.             surface.repaint();
  199.         }
  200.  
  201.         if (verboseFlag) {
  202.            surface.verbose();
  203.         }
  204.     }
  205.  
  206.  
  207.     public Dimension getPreferredSize() {
  208.         return new Dimension(200,38);
  209.     }
  210.  
  211.  
  212.     public void start() {
  213.         thread = new Thread(this);
  214.         thread.setPriority(Thread.MAX_PRIORITY);
  215.         thread.setName("Printing " + surface.name);
  216.         thread.start();
  217.     }
  218.  
  219.  
  220.     public synchronized void stop() {
  221.         thread = null;
  222.         notifyAll();
  223.     }
  224.  
  225.  
  226.     public void run() {
  227.         boolean stopped = false;
  228.         if (surface.thread != null) {
  229.             stopped = true;
  230.             startStopB.doClick();
  231.         }
  232.         PrinterJob printJob = PrinterJob.getPrinterJob();
  233.         printJob.setPrintable(surface);
  234.         if (printJob.printDialog()) {
  235.             try {
  236.                 printJob.print();
  237.             } catch (Exception ex) {
  238.                 ex.printStackTrace();
  239.             }
  240.         }
  241.         if (stopped) {
  242.             startStopB.doClick();
  243.         }
  244.         thread = null;
  245.     }
  246.  
  247. }
  248.